package bar.dark.lee.alarm.beginalarm;

import bar.dark.lee.alarm.effect.EffectActivity;
import bar.dark.lee.alarm.effect.EffectListActivity;
import bar.dark.lee.alarm.effect.question.EffectQuestionActivity;
import bar.dark.lee.alarm.effect.typing.EffectTypingActivity;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class BDAlarmService extends Service
{
	@Override
	public void onStart(Intent intent, int startId)
	{
		super.onStart(intent, startId);
		
		String position = intent.getStringExtra(BDAlarmManager.ALERM_FLAG_POS);
		
		if (position == null)
		{
			position = -1 + "";
		}
		
		String [] positions = position.split(",");
		String firstPos = "";
		
		if (positions.length > 1)
		{
			firstPos = positions[Math.round((float)(Math.random() * (positions.length-1)))];
		}
		else
		{
			firstPos = positions[0];
		}
		
		startAlarmActivity(Integer.parseInt(firstPos), getApplicationContext());
	}
	
	@Override
	public IBinder onBind(Intent arg0)
	{
		return null;
	}
	
	private void startAlarmActivity(int position, Context Context)
	{
		Intent intent = new Intent();

		switch (position) 
		{
			case 0:
				intent.setClass(Context, EffectQuestionActivity.class);
				intent.putExtra(EffectListActivity.EFFECT_PLAY, EffectListActivity.EFFECT0);
				break;
			case 1:
				intent.setClass(Context, EffectTypingActivity.class);
				intent.putExtra(EffectListActivity.EFFECT_PLAY, EffectListActivity.EFFECT1);
				break;
			case 2:
				intent.setClass(Context, EffectQuestionActivity.class);
				intent.putExtra(EffectListActivity.EFFECT_PLAY, EffectListActivity.EFFECT0);
				break;
			case 3:
				intent.setClass(Context, EffectQuestionActivity.class);
				intent.putExtra(EffectListActivity.EFFECT_PLAY, EffectListActivity.EFFECT0);
				break;
			case 4:
				intent.setClass(Context, EffectQuestionActivity.class);
				intent.putExtra(EffectListActivity.EFFECT_PLAY, EffectListActivity.EFFECT0);
				break;
			case 5:
				intent.setClass(Context, EffectQuestionActivity.class);
				intent.putExtra(EffectListActivity.EFFECT_PLAY, EffectListActivity.EFFECT0);
				break;
			default:
				break;
		}
		
		intent.putExtra(EffectListActivity.EFFECT_SELECT, position);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//		intent.putExtra(EffectActivity.FLAG_TEST_REAL, true);
		startActivity(intent);
	}
}
